class Wf {
	constructor() {
		this.moduleMap = {};
		this.moduleLoadedQueue = {
			test: [],
		};
	}

	getModule(name) {
		return this.moduleMap[name];
	}

	addModule(name, module) {
		this.moduleMap[name] = module;
	}

	// 同步安装模块
	use(module) {
		module.install(this);
	}

	// 异步安装模块
	async useAsync(name, module) {
		const install = module.install;
		const m = await install(this);

		// 清除队列
		this.resolveModuleQueue(name);

		return m;
	}

	resolveModuleQueue(moduleName) {
		const queue = this.moduleLoadedQueue[moduleName];
		const module = this.moduleMap[moduleName];
		if (queue && queue.length > 0) {
			queue.forEach((func) => {
				func(module);
				queue.shift();
			});
		}
	}

	onModuleLoad(name, callback) {
		const queue = this.moduleLoadedQueue[name];
		if (!queue) {
			this.moduleLoadedQueue[name] = [];
		}
		this.moduleLoadedQueue[name].push(callback);
	}
}
export default Wf;
